package lgkj.meicqt20.utils.tilepuzzle;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.os.Environment;
import android.util.Log;

import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.Logger;
import com.umeng.commonsdk.UMConfigure;

import java.io.File;
import java.util.Random;

import lgkj.meicqt20.R;
import lgkj.meicqt20.service.MyService;
import lgkj.meicqt20.utils.AllKindsOfConstant;

public class ClassBoardModel extends Application {
    public static int gameType = 0;
    public boolean isResultSucess = false;
    private static Context context;
    //    public static String getSession() {
//        String   mac= CommUtils.getLocalMacAddressFromWifiInfo(get);
//        if (session != null) {
//            return session;
//        }
//        return "mm426ncimmsgiq9nl25stkhmq2";
//    }
    private String TAG = "ClassBoardModel";
    private String folderPath = Environment.getExternalStorageDirectory() + File.separator + "lianguang-Logcat" + File.separator;
    private static ClassBoardModel insteance;
    public String[] gameSetData;
    public Bitmap orgImage;
    public int rows;// 行大小
    public int columns;// 列大小
    //   Image images[][] ;
    /**
     * 空方格类
     */
    public ClassPiece blankp; // 空方格类
    public ClassPiece[] all;// 所有方格类
    public ClassPiece[][] grid;// 二维方格类
    public Random rand;// 随机类

    // cell geometry in pixels
    public int cellWidth;// 方格的宽
    public int cellHeight;// 方格的高

    // 窗口的宽度
    private int screenWidth = 0;
    // 窗口的高度
    private int screenHeight = 0;

    // 背景图片的宽度
    private int photoWidth = 720;
    // 背景图片的高度
    private int photoHeight = 800;    //图片像素必须大于720*800
    /**
     * 方格里面图片之间的空隙像素大小
     */
    public int spaces = 1;

    /**
     * 屏幕的水平偏移量
     */
    private int screenOffset_x = 0;
    /**
     * 屏幕的垂直偏移量
     */
    private int screenOffset_y = 0;

    /**
     * 图片的水平偏移量
     */
    private int photoOffset_x = 0;
    /**
     * 图片的垂直偏移量
     */
    private int photoOffset_y = 0;

    //Command[] cmd;// 命令数组
    public int gameState;// 游戏状态
    public boolean cheated;


    public long starTime = 0;

    public long sumTime = 0;

    public void setScreenWidth(int screenWidth) {
        this.screenWidth = screenWidth;
    }

    public void setScreenHeight(int screenHeight) {
        this.screenHeight = screenHeight;
    }

    public static Context getContext() {
        return context;
    }

    public void initGameData() {
        Log.i(TAG, "columns：" + columns + "，rows：" + rows);
        rows = 3;
        columns = 3;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        insteance = this;
        context = getApplicationContext();
        Logger.addLogAdapter(new AndroidLogAdapter());
        startService(new Intent(this, MyService.class));

        UMConfigure.init(this, UMConfigure.DEVICE_TYPE_PHONE, "");
    }

    public static synchronized ClassBoardModel getInsteance() {
        return insteance;
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
    }

    public void initData() {
        initGameData();
        System.out.println("BoardModel:初始化拼图信息");
        cheated = false;
        rand = new Random();

        // create the grid arrays
        grid = new ClassPiece[columns][rows];
        all = new ClassPiece[rows * columns];
        initPhotoData();
    }
    public void initPhotoData() {
        String name = "";
        try {
            name = AllKindsOfConstant.NEW_VERSION_PATH + AllKindsOfConstant.puzzleName[gameType];
            orgImage = BitmapFactory.decodeFile(name);
        } catch (Exception e) {
            Log.d(TAG, "initPhotoData: " + e.getMessage());
            orgImage = ((BitmapDrawable) getResources().getDrawable(Integer.valueOf(R.mipmap.focus))).getBitmap(); //得到图片流
        }
        changeOffsetPhotoWH();
        Log.i(TAG, "initPhotoData: " + name + ",gameType:" + gameType);
        cellWidth = photoWidth / rows;
        cellHeight = photoHeight / columns;
        Bitmap temp;
        int i, j;
        /**
         * @x 横坐标
         * @y 列坐标
         * */
        int x = 0, y = 0;
        for (i = 0; i < columns; ) {
            x = 0;
            for (j = 0; j < rows; ) {
                //grid[i][j].img ;
                System.out.println(x + "," + y + "," + cellWidth + "," + cellHeight);
                try {
                    temp = Bitmap.createBitmap(
                            orgImage,
                            photoOffset_x + spaces + x,
                            photoOffset_y + spaces + y,
                            cellWidth - spaces,
                            cellHeight - spaces
                    );
                } catch (Exception e) {
                    Log.i(TAG, "initPhotoData: 图片尺寸有问题");
                    orgImage = ((BitmapDrawable) getResources().getDrawable(Integer.valueOf(R.mipmap.focus))).getBitmap();
                    temp = Bitmap.createBitmap(
                            orgImage,
                            photoOffset_x + spaces + x,
                            photoOffset_y + spaces + y,
                            cellWidth - spaces,
                            cellHeight - spaces
                    );
                }
                grid[i][j] = all[i * rows + j] = new ClassPiece(temp, i * rows + j, i, j, cellWidth, cellHeight, screenOffset_x, screenOffset_y, spaces);
                x = (++j) * cellWidth;
            }
            y = (++i) * cellHeight;
        }
        Bitmap blankImage;//=grid[columns-1][rows-1].bitmap;//Bitmap.createBitmap(cellWidth,cellHeight, Config.ARGB_8888);
        if (isResultSucess) {
            blankImage = grid[columns - 1][rows - 1].bitmap;
        } else {
            blankImage = ClassPhotoOptions.createBlankBitmap(cellWidth - spaces, cellHeight - spaces, null);
        }
        //  blankImage=Bitmap.createBitmap(colors, width, height, config)
        blankp = new ClassPiece(blankImage, (rows * columns) - 1, columns - 1, rows - 1, cellWidth, cellHeight, screenOffset_x, screenOffset_y, spaces);
        blankp.isblank = true;
        grid[columns - 1][rows - 1] = blankp;
    }

    private void changeOffsetPhotoWH() {
        //如果图片的宽度大于屏幕的宽度
        if (photoWidth > screenWidth) {
            photoWidth = screenWidth;
            screenOffset_x = 0;
            photoOffset_x = (photoWidth - screenWidth) / 2;
            //如果图片的高度大于高幕的高度
            if (photoHeight > screenHeight) {
                photoHeight = screenHeight;
                screenOffset_y = 0;
                orgImage = Bitmap.createBitmap(orgImage, (photoWidth - screenWidth) / 2, 0, screenWidth, screenHeight);
            }
            //如果图片的高度小于屏幕的高度
            else if (photoHeight < screenHeight) {
                screenOffset_y = (screenHeight - photoHeight) / 2;
                orgImage = Bitmap.createBitmap(orgImage, (photoWidth - screenWidth) / 2, 0, screenWidth, photoHeight);
            } else ;

        }
        //如果图片的宽度小于屏幕的宽度
        else if (photoWidth < screenWidth) {
            screenOffset_x = (screenWidth - photoWidth) / 2;
            //如果图片的高度大于高幕的高度
            if (photoHeight > screenHeight) {
                photoHeight = screenHeight;
                screenOffset_y = 0;
                orgImage = Bitmap.createBitmap(orgImage, 0, 0, photoWidth, screenHeight);
            }
            //如果图片的高度小于屏幕的高度
            else if (photoHeight < screenHeight) {
                screenOffset_y = (screenHeight - photoHeight) / 2;
            } else ;
        } else {
            photoOffset_x = 0;
            photoOffset_y = 0;
            screenOffset_x = 0;
            screenOffset_y = 0;
        }
    }

    public void addString() {
        int j = 0, i = 0;
        for (i = 0; i < columns; i++) {
            for (j = 0; j < rows; j++) {
                grid[i][j].addString = true;
            }
        }

        for (i = 0; i < rows * columns; i++) {
            all[i].addString = true;
        }
    }

    public void removeString() {
        int j = 0, i = 0;
        for (i = 0; i < columns; i++) {
            for (j = 0; j < rows; j++) {
                grid[i][j].addString = false;
            }
        }

        for (i = 0; i < rows * columns; i++) {
            all[i].addString = false;
        }
    }


    //把P类赋值给(x,y),更改坐标为(x,y)
    void setGrid(ClassPiece p, int x, int y) {
        grid[x][y] = p;
        grid[x][y].setLocation(x, y);
    }

    // swap the piece at sx, sy with the blank piece
    // assumes that this is a legal move
    public void moveBlank(int swapx, int swapy) {
        // System.out.println("黑块移动时输出： "+(blankp.x+1)+","+ (blankp.y+1)+"，"+(swapx+1)+","+(swapy+1));
        swap(swapx, swapy, blankp.x, blankp.y);
        // setGrid(grid[swapx][swapy], blankp.x, blankp.y);
        //setGrid(blankp, swapx, swapy);
    }

    // swaps the pieces at (x1, y1) and (x2, y2)
    // no parity checking is done!
    void swap(int x1, int y1, int x2, int y2) {
        ClassPiece t = grid[x1][y1];
        setGrid(grid[x2][y2], x1, y1);
        setGrid(t, x2, y2);
    }

    public boolean isSolved() {
        for (int i = 0; i < columns; i++) {
            for (int j = 0; j < rows; j++) {
                if (!grid[i][j].isHome()) {
                    return false;
                }
            }
        }

        return true;
    }

    /**
     * 重画空块与指定块交换后的界面
     *
     * @canvas 画布
     * @x 指定块的所在行
     * @y 指定块的所在列
     * @strHeight 添加行列坐标的字体高
     */
    public boolean repaintBlank(Canvas canvas, int x, int y, int strHeight) {
        grid[x][y].paint(canvas, strHeight);
        grid[blankp.x][blankp.y].paint(canvas, strHeight);
        return true;
    }

    // return a random integer in the range [0..n)
    int randRange(int n) {
        int r = rand.nextInt() % n;

        if (r < 0) {
            r += n;
        }
        return r;
    }

    // shuffle the tiles randomly and place the blank at the bottom right
    void shuffle() {
        int limit = (rows * columns) - 1;

        ClassPiece[] ta = new ClassPiece[limit];
        ClassPiece temp;

        System.arraycopy(all, 0, ta, 0, limit);
        //添加奇偶性判断
        //直到为偶
        while (true) {
            for (int i = 0; i < limit; i++) {
                int j = randRange(limit);
                temp = ta[j];
                ta[j] = ta[i];
                ta[i] = temp;
            }

            for (int i = 0; i < limit; i++) {
                setGrid(ta[i], i / rows, i % rows);
            }

            setGrid(blankp, columns - 1, rows - 1);
            //如果判定为偶，跳出
            if (ClassSerialCheck.isEven(ta)) {
                //  System.out.println("判断为 偶 ！");
                break;
            } else {
                // System.out.println("判断为 奇 ！重新洗牌。。。。。。");
                resetGrid();
                continue;
            }
        }

    }

    public void randomize(boolean hard) {

        shuffle();
        int ra;
        int rb;
        int x;
        int y;

        if (hard) {
            ra = 7;
            rb = 0;
        } else {
            ra = 0;
            rb = 7;
        }

        x = rand.nextInt() & 1;
        y = rand.nextInt() & 1;

        if ((x == 1) && (y == 1)) {
            x = 2;
            y = 0;
        }
        swap(x, y, all[ra].x, all[ra].y);
        swap((rand.nextInt() & 1) + 1, 2, all[rb].x, all[rb].y);

        if ((displacement() & 1) == 1) {
            swap(1, rows - 1, rows - 1, rows - 1);
        }

    }

    // Compute and return the displacement, that is, the number of
    // pairs of tiles that are out of order.  The blank tile *must*
    // be in the lower right corner.
    int displacement() {
        boolean[] temp = new boolean[(rows * columns) - 1]; // all false
        int n = 0;

        for (int i = 0; i < columns; i++) {
            for (int j = 0; j < rows; j++) {
                ClassPiece p = grid[i][j];

                if (p == blankp) {
                    continue;
                }

                temp[p.serial] = true;

                for (int k = 0; k < p.serial; k++) {
                    if (!temp[k]) {
                        n++;
                    }
                }
            }
        }

        return n;
    }

    public void resetGrid() {
        ClassPiece[] temp = new ClassPiece[rows * columns];
        int k = 0;

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                temp[k++] = grid[j][i];
            }
        }

        for (k = 0; k < temp.length; k++) {
            temp[k].goHome();
        }
    }

    //有趣的洗牌
    public void rearrangeFunnily(boolean hard) {
        resetGrid();

        if (hard) {
            swap(0, 0, columns - 1, 1);
            swap(columns - 1, rows - 2, columns - 1, 2);
            swap(columns - 1, rows - 1, 0, rows - 1);
            swap(0, rows - 1, rows - 1, rows - 1);
        } else {
            swap(0, 0, columns - 1, rows - 1);
            swap(columns - 1, 0, 0, rows - 1);
        }
    }
}
